package com.woniu.zhoukao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.zhoukao.entity.Product;
import com.woniu.zhoukao.es.ESProductRepository;
import com.woniu.zhoukao.mapper.ProductMapper;
import com.woniu.zhoukao.service.ProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.zhoukao.vo.EsProduct;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liuao
 * @since 2023-02-07 09:47:31
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {
    @Autowired
    ProductMapper productMapper;

    @Autowired
    ESProductRepository esProductRepository;

    @Override
    public Page<Product> pageData(Integer currentPage, Integer pageSize) {

        Page<Product> productPage = new Page<>(currentPage,pageSize);

        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();

        Page<Product> pageData = productMapper.selectPage(productPage, queryWrapper);

        return pageData;
    }

    @Override
    public Page<Product> search(Integer currentPage, Integer pageSize, String keyword) {

        Page<Product> productPage = new Page<>(currentPage, pageSize);

        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("productname", keyword);

        Page<Product> pageData = productMapper.selectPage(productPage, queryWrapper);

        return pageData;
    }


    @Override
    public Page<Product> searchBookBySql(Integer currentPage, Integer pageSize, String keyword) {

        Page<Product> page = new Page<>(currentPage,pageSize);

        //多字段模糊查询
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name",keyword)
                .or()
                .like("provider",keyword);

        Page<Product> bookPageResult =productMapper.selectPage(page, queryWrapper);

        return bookPageResult;
    }

    @Override
    public Page<EsProduct> searchBookByES(Integer currentPage, Integer pageSize, String keyword) {

        //构建查询条件
        NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();

        //query
        nativeSearchQueryBuilder.withQuery(QueryBuilders.multiMatchQuery(keyword,"name", "price"));

        //分页条件
        //注意: es中的分页是从第0页开始的
        nativeSearchQueryBuilder.withPageable(PageRequest.of(currentPage-1,pageSize));

        //排序
        nativeSearchQueryBuilder.withSort(SortBuilders.fieldSort("buycount").order(SortOrder.DESC));

        org.springframework.data.domain.Page<EsProduct> esProductPage = esProductRepository.search(nativeSearchQueryBuilder.build());

        //将 Es的page 转换 mybatisPlus的 page
        Page<EsProduct> page = new Page<>(currentPage,pageSize);
        page.setPages(esProductPage.getTotalPages());
        page.setTotal(esProductPage.getTotalElements());
        page.setRecords(esProductPage.getContent());

        return page;
    }
}
